﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using BIExtAuth.BLL;
using System.Data;
using BIExtAuth.Model;
using BIExtAuth.Common;
using System.IO;

namespace BIExtAuth.Web
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class ExtAuthServer : System.Web.Services.WebService
    {
        UserBLL userBLL = new UserBLL();

        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="password">密码</param>
        /// <returns>0：验证通过，-1：验证失败</returns>
        [WebMethod]
        public int Login(string userId, string password)
        {
            int ret = -1;
            
            if (checkUserIdPassword(userId, password))
                ret = 0;
            else
                ret = -1;

            return ret;
        }

        /// <summary>
        /// 验证用户ID和密码
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="password">密码</param>
        /// <returns>true：验证通过，false：验证失败</returns>
        public bool checkUserIdPassword(string userId, string password)
        {
            bool ret = false;
            ret = this.userBLL.CheckUser(userId, SecurityEncrypt.DesEncrypt(password));
            return ret;
        }

        /// <summary>
        /// 获取域
        /// </summary>
        /// <returns>域集合</returns>
        [WebMethod]
        public string[] GetDomains()
        {
            return new string[] { "盖雅信息技术有限公司" };
        }

        /// <summary>
        /// 获取域中的组
        /// </summary>
        /// <param name="domainName">域名</param>
        /// <returns>域下所有组的集合</returns>
        [WebMethod]
        public string[] GetDomainGroups(string domainName)
        {
            return new string[] { "盖雅信息技术有限公司" };
        }

        /// <summary>
        /// 获取组中的用户
        /// </summary>
        /// <param name="groupName">组名</param>
        /// <returns>组下所有的用户集合</returns>
        [WebMethod]
        public string[] GetGroupUsers(string groupName)
        {
            List<string> userlist = new List<string>();
            DataSet ds = this.userBLL.GetAllUsers();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                userlist.Add(dr["UserID"].ToString());
            }
            return userlist.ToArray();
        }

        /// <summary>
        /// 根据用户ID查询用户
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>BI用户</returns>
        [WebMethod]
        public ExtUser FindUser(string userId)
        {
            BIUser user = this.userBLL.GetUserByUserID(userId);
            if (user == null)
            {
                return ExtUser.Empty;
            }
            else
            {
                return new ExtUser(user.UserID, user.UserName, user.UserDesc, user.UserEmail);
            }
        }

        /// <summary>
        /// 根据用户ID查询所属的组
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>用户所属的组</returns>
        [WebMethod]
        public string[] GetUserGroups(string userId)
        {
            return new string[] { "盖雅信息技术有限公司" };
        }

        /// <summary>
        /// 根据用户ID查询用户EMail
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>用户EMail</returns>
        [WebMethod]
        public string GetUserEmailAddress(string userId)
        {
            BIUser user = this.userBLL.GetUserByUserID(userId);
            if (user == null)
            {
                return string.Empty;
            }
            else
            {
                return user.UserEmail;
            }
        }
    }

    /// <summary>
    /// ExtUser
    /// </summary>
    public class ExtUser
    {
        public string UserId { get; set; }
        public string FullName { get; set; }
        public string Description { get; set; }
        public string Email { get; set; }
        public ExtUser()
        {
        }
        public ExtUser(string userId, string fullName, string description, string email)
        {
            UserId = userId;
            FullName = fullName;
            Description = description;
            Email = email;
        }
        public static ExtUser Empty
        {
            get { return new ExtUser(string.Empty, string.Empty, string.Empty, string.Empty); }
        }
    }
}
